package com.example.visitedsys.Controller;

import com.example.visitedsys.Shrio.Bean.ResponseBean;
import com.example.visitedsys.informationGet.LogVO;
import com.example.visitedsys.informationGet.PageVO;
import com.example.visitedsys.pojo.Log;
import com.example.visitedsys.service.LogService;
import com.wuwenze.poi.ExcelKit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 〈操作日志〉<br>
 * 〈〉
 */

/**
 * @author  SongPeng
 * @date  2021/11/9 14:19
 * @version 1.0
 */

@RestController
@RequestMapping("/log")
public class LogController {
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","操作日志模块");
    }

    private static final Logger logger = LogManager.getLogger(LogController.class);
    @Autowired
    private LogService logService;
    /**
     * 日志列表
     *
     * @return
     */
    @RequiresPermissions("operateLog:select")
    @GetMapping("/findLogList")
    public ResponseBean findLogList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize") Integer pageSize,
                                    LogVO logVO) {
        PageVO<LogVO> logList = logService.findLogList(pageNum, pageSize, logVO);
        return ResponseBean.success(logList);
    }
    /**
     * 删除操作日志
     *
     * */
    @RequiresPermissions("operateLog:delete")
    @DeleteMapping("/delete/{operateId}")
    public ResponseBean deleteById(@PathVariable Long operateId){
        logger.info("删除一条操作日志");
        logService.deleteById(operateId);
        return ResponseBean.success();
    }

    /**
     *  备份操作日志excel
     * @param response
     */
    @PostMapping("/excel")
    @RequiresPermissions("operateLog:save")
    public void export(HttpServletResponse response) {
        logger.info("备份操作日志");
        List<Log> logs = this.logService.findAll();
        ExcelKit.$Export(Log.class, response).downXlsx(logs, false);
    }
    /**
     * 批量删除
     * @param operateIds
     * @return
     */
    @RequiresPermissions({"operateLog:batchDelete"})
    @DeleteMapping("/batchDelete/{operateIds}")
    public ResponseBean batchDelete(@PathVariable String operateIds) {
        logger.info("批量删除日志");
        String[] idList = operateIds.split(",");
        List<Long> list = new ArrayList<>();
        if (idList.length > 0) {
            for (String s : idList) {
                list.add(Long.parseLong(s));
            }
        }
        logService.batchDelete(list);
        return ResponseBean.success("批量删除成功");
    }


}

